﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace RehabilitationRobot
{
    public partial class JogForm : Form
    {
        #region 变量定义
        private int JogAxisNum;
        private int JogDirection;
        private double JogVel;
        Thread AxisJogThread;
        #endregion
       
        public JogForm()
        {
            InitializeComponent();
        }
       
        /* 点动线程托管函数 */
        private void JogThread()
        {
            Form1.MotionController.RobotMoveAllow();
            while(true)
            {
                Form1.MotionController.AxisJog(JogAxisNum,JogDirection,JogVel);
            }
        }

        /* 窗体关闭事件 */
        private void JogForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("请确保机器停止运行", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                e.Cancel = false;
                Form1.JogFormShow = false;
            }
            else
            {
                e.Cancel = true;
            }  
        }

        /* 全部回零 */
        private void btn_AllAxisToZero_Click(object sender, EventArgs e)
        {
            Form1.MotionController.GotoZero();
        }

        #region 左髋点动
        private void btn_HLJOGClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 1;
            JogDirection = -1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_HLJOGClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }

        private void btn_HLJOGAntiClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 1;
            JogDirection = 1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_HLJOGAntiClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }
        #endregion
      

        #region 右髋点动
        private void btn_HRJOGClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 2;
            JogDirection = -1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_HRJOGClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }

        private void btn_HRJOGAntiClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 2;
            JogDirection = 1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_HRJOGAntiClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }

        #endregion


        #region 左膝点动

        private void btn_KLJOGClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 3;
            JogDirection = -1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_KLJOGClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }

        private void btn_KLJOGAntiClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 3;
            JogDirection = 1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_KLJOGAntiClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }
        #endregion


        #region 右膝点动
        private void btn_KRJOGClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 4;
            JogDirection = -1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_KRJOGClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }

        private void btn_KRJOGAntiClockwise_MouseDown(object sender, MouseEventArgs e)
        {
            JogAxisNum = 4;
            JogDirection = 1;
            JogVel = Jog_Vel_Bar.Value;
            AxisJogThread = new Thread(new ThreadStart(JogThread));
            AxisJogThread.Start();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = true;
        }

        private void btn_KRJOGAntiClockwise_MouseUp(object sender, MouseEventArgs e)
        {
            AxisJogThread.Abort();
            Form1.MotionController.RobotMoveForbid();

            /* 点动状态 */
            Form1.MotionController.UpperComputer_State.JogMode_Running = false;
        }

        #endregion

        private void btn_ClearDh_Click(object sender, EventArgs e)
        {
            Form1.MotionController.DhClear();
        }

        private void btn_RecFromLimit_Click(object sender, EventArgs e)
        {
            Form1.MotionController.ParkerProgram2Stop();
            Form1.MotionController.ServoOn();   
        }

        private void Jog_Vel_Bar_ValueChanged(object sender, EventArgs e)
        {
            label_JogVelocity.Text = UIThread.ReadTrackBarValue(Jog_Vel_Bar).ToString();
        }
      
    }
}
